package interview

import kotlinetc.println
import org.omg.CORBA.INTERNAL

//FaceBook on-site interview
//https://leetcode.com/discuss/interview-experience/301164/facebook-onsite-interview-experience-2019


//Google
//https://leetcode.com/discuss/interview-question/309064/google-phone-interview-remove-repeating-numbers


//Google
//Compress 2D Array
//https://leetcode.com/discuss/interview-question/326564/google-onsite-interview-compress-2d-array

data class Box constructor(var number: Int, var row: Int, var col: Int)

fun main(args: Array<String>) {
    compress2dArray(arrayOf(
            intArrayOf(7, 6),
            intArrayOf(4, 9)
    )).asList().forEach {
        it.asList().println()
    }
}


fun compress2dArray(nums: Array<IntArray>): Array<IntArray> {

    //记录原有位置

    val rows = nums.size
    val cols = nums[0].size

    val nums1 = Array(rows * cols, {
        Box(0, 0, 0)
    })

    for (i in 0 until rows)
        for (j in 0 until cols) {
            val box = nums1[i * cols + j]
            box.number = nums[i][j]
            box.row = i
            box.col = j
        }

    nums1.sortBy { it.number }

    nums1.forEachIndexed({ index, value ->
        nums[value.row][value.col] = index+1
    })
    return nums
}